<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/修改游戏</title>
<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" action="" id="saveAndUpdateFrom" target="_parent"  enctype="multipart/form-data">
			<div class="form-group">
				<label class="col-xs-2 text-right">游戏:</label>
				<div class="col-xs-4">
					<sk:select cssClass="form-control select" id="gameId" name="gameId" value="${page.wm.ROW_ID}" requiredInit="true" ajaxURL="${userInfoV1}/baseGame/gameSelect?wm[USE_FLAG]=1&wm[DEL_FLAG]=1" onchange="TagUtil.fillOptions('gameId');">
						<option value="">请选择</option>
					</sk:select>
					<input type="hidden" name="gameName" id="gameName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">区:</label>
				<div class="col-xs-4">
					<sk:select cssClass="form-control select" id="areaId" name="areaId" value="${page.wm.ROW_ID}" requiredInit="true" ajaxURL="${userInfoV1}/baseArea/select?wm[USE_FLAG]=1&wm[DEL_FLAG]=1" parentCascadedSelect="gameId" parentParamName="gameId" onchange="TagUtil.fillOptions('areaId');" >
						<option value="">请选择</option>
					</sk:select>
					<input type="hidden" name="areaName" id="areaName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">服:</label>
				<div class="col-xs-4">
					<sk:select cssClass="form-control select" id="serverId" name="serverId" value="${page.wm.ROW_ID}" requiredInit="true" ajaxURL="${userInfoV1}/baseServer/select?wm[USE_FLAG]=1&wm[DEL_FLAG]=1" parentCascadedSelect="areaId" parentParamName="areaId">
						<option value="">请选择</option>
					</sk:select>
					<input type="hidden" name="serverName" id="serverName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">当前物品:</label>
				<div class="col-xs-4">
					<sk:select cssClass="form-control select" id="currentGoodsId" name="currentGoodsId" value="${page.wm.ROW_ID}" requiredInit="true" ajaxURL="${userInfoV1}/gameGoodsType/smallTypeSelect?wm[TYPE_FLAG]=10">
						<option value="">请选择</option>
					</sk:select>
					<input type="hidden" name="currentGoods" id="currentGoods">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">置换物品:</label>
				<div class="col-xs-4">
					<sk:select cssClass="form-control select" id="targetGoodsId" name="targetGoodsId" value="${page.wm.ROW_ID}" requiredInit="true" ajaxURL="${userInfoV1}/gameGoodsType/smallTypeSelect?wm[TYPE_FLAG]=10">
						<option value="">请选择</option>
					</sk:select>
					<input type="hidden" name="targetGoods" id="targetGoods">
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-2 text-right">更新方式:</label>
				<div class="col-xs-7">
					<td colspan="2">
						<label for="port"><input name="updateFlag" type="radio" checked  value="10"/>&nbsp;手动维护</label>&nbsp;&nbsp;&nbsp;&nbsp;
						<label for="no"><input name="updateFlag" type="radio" value="20"/>&nbsp;自动更新</label>
					</td>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">置换数量:</label>
				<div class="col-xs-7">
					<input type="text" class="input" name="swapNum" id="swapNum" value=""  placeholder="请输入置换数量">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">排序号:</label>
				<div class="col-xs-7">
					<input type="text" class="input" name="orderByNo" id="orderByNo" value=""  placeholder="请输入排序编号">
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-2 text-right">是否开启:</label>
				<div class="col-xs-7">
					<td colspan="2">
	                   	<label for="port"><input id="yse" name="openFlag" type="radio" value="1"/>&nbsp;是</label>&nbsp;&nbsp;&nbsp;&nbsp;
	                   	<label for="no"><input id="no" name="openFlag" type="radio" value="0"/>&nbsp;否</label>
	               	</td>
				</div>
			</div>
			<input type="hidden" id="rowId" name="rowId" value="<%=request.getParameter("rowId")%>">
           	<input type="hidden" id="token" name="token" value="">
           	<input type="hidden" id="gameIcon" name="gameImgPage" value="${tlzsGame.gameIcon}">
           	<input type="hidden" id="flag"  value="<%=request.getParameter("flag")%>">
           	<input type="hidden" id="isReCommit" value="0">
		</sk:form>	
	</div>
</body>
<%@ include file="/view/common/footer.jsp"%>
<script type="text/javascript">
$(function(){
    $("#token").val(sessionStorage.getItem("token"));
    var rowId = $("#rowId").val();
    if(rowId > 0){
        $.ajax({
            url: "${userInfoV1}/gameGoodsSwap/getGameGoodsSwapInfo?r="+Math.random(),
            type: "post",
            cache: false,
            data:{"rowId":rowId},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data == null) {
                    return;
                }
                $("#gameId").val(data.gameId);
                $("#gameName").val(data.gameName);
                $("#areaId").val(data.areaId);
                $("#areaId").html(data.areaName);
                $("#areaName").val(data.areaName);
                $("#serverId").val(data.serverId);
                $("#serverName").val(data.serverName);
                $("#currentGoodsId").val(data.currentGoodsId);
                $("#currentGoods").val(data.currentGoods);
                $("#targetGoodsId").val(data.targetGoodsId);
                $("#targetGoods").val(data.currentGoods);
                $("#swapNum").val(data.swapNum);
                $("#orderByNo").val(data.orderByNo);

                if(data.openFlag == 1){
                    $("#yse").attr("checked","true");
                    $("#no").removeAttr("checked");
                } else {
                    $("#yse").removeAttr("checked");
                    $("#no").attr("checked","true");
                }
                fillAreaServer(data.gameId,data.areaId);
            }
        });
    }
})


function fillAreaServer(gameId, areaId){
    var token = $("#token").val();
    if(gameId > 0){
        $.ajax({
            url: " ${userInfoV1}/baseArea/select?wm[USE_FLAG]=1&wm[DEL_FLAG]=1",
            type: "post",
            cache: false,
            data:{"gameId":gameId,"token":token},
            dataType:"json",
            async:false,
            success: function(data) {
                console.log(gameId);
                console.log(data);
                if (data == null) {
                    return;
                }
                $("#areaId").empty();
                $.each(data, function (index, item) {
                    console.log(item);
                    var id = item.value;
                    var text = item.label;
                    var option = "<option value='"+id+"' "
                    if (areaId == id){
                        option += " selected = 'selected'  ";
                    }
                    option += " >"+text+"</option>"
                    $("#areaId").append(option);
                });
            }
        });
    }

    if(areaId > 0){
        $.ajax({
            url: " ${userInfoV1}/baseServer/select?wm[USE_FLAG]=1&wm[DEL_FLAG]=1",
            type: "post",
            cache: false,
            data:{"areaId":areaId,"token":token},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data == null) {
                    return;
                }
                $("#serverId").empty();
                $.each(data, function (index, item) {
                    var id = item.value;
                    var text = item.label;
                    var option = "<option value='"+id+"' "
                    if (serverId == id){
                        option += " selected = 'selected'  ";
                    }
                    option += " >"+text+"</option>"
                    $("#serverId").append(option);
                });
            }
        });
    }
}


var err = {'1':'添加','2':'修改'};
var action = {'1':'/save','2':'/update'};
function add(){
    var rowId = $('#rowId').val();
    var i;
    if (!CheckUtil.isNotNull(rowId) || rowId == 0) {
        i = 1;//添加
    } else {
        i = 2;//修改
    }
    var gameId = $.trim($("#gameId").val());
    if (!CheckUtil.isNotNull(gameId)) {
        layer.msg("请选择游戏!");
        return;
    }

	var gameName = $("#gameId").find("option:selected").text();
    $("#gameName").val(gameName);


    var areaId = $.trim($("#areaId").val());
    if (!CheckUtil.isNotNull(areaId)) {
        layer.msg("请选择区!");
        return;
    }

    var areaName = $("#areaId").find("option:selected").text();
    $("#areaName").val(areaName);

    var serverId = $.trim($("#serverId").val());
    if (!CheckUtil.isNotNull(serverId)) {
        layer.msg("请选择服!");
        return;
    }

    var serverName = $("#serverId").find("option:selected").text();
    $("#serverName").val(serverName);
    
    var currentGoodsId = $("#currentGoodsId").val();
    if (!CheckUtil.isNotNull(currentGoodsId)) {
        layer.msg("请选择当前物品!");
        return;
    }
    var currentGoods = $("#currentGoodsId").find("option:selected").text();
    $("#currentGoods").val(currentGoods);

    var targetGoodsId = $("#targetGoodsId").val();
    if (!CheckUtil.isNotNull(targetGoodsId)) {
        layer.msg("请选择目标物品!");
        return;
    }
    var targetGoods = $("#targetGoodsId").find("option:selected").text();
    $("#targetGoods").val(targetGoods);


    var swapNum = $('#swapNum').val();
    if (!CheckUtil.isNotNull(swapNum)) {
        layer.msg("置换数量不能为空!");
        return;
    }
    var orderByNo = $('#orderByNo').val();
    if (!CheckUtil.isNotNull(orderByNo)) {
        layer.msg("排序号不能为空!");
        return;
    }
    var openFlag =$('input:radio[name="openFlag"]:checked').val();
    if (!CheckUtil.isNotNull(openFlag)) {
        layer.msg("请选择是否开启!");
        return;
    }

    var gameIsExist = checkGameName();
    if (gameIsExist) {
        layer.msg("数据已存在");
        return false;
    }

    //loading层
    var index = layer.load(0, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
    
    $.ajax({
        url: "${userInfoV1}/gameGoodsSwap" + action[i],
        type: "post",
        cache: false,
        //data:parseJson(decodeURIComponent($("#saveAndUpdateFrom").serialize())),
        data: new FormData($("#saveAndUpdateFrom")[0]),
        dataType:"json",
        processData: false,
        contentType: false,
        async:false,
        success: function(data) {
            layer.close(index);
            if(data.code == 0){
                parent.layer.msg("添加成功！");
            } else if(data.code == 1){
                parent.layer.msg("修改成功！");
            } else if(data.code == "-2"){
                parent.window.location.href="${sysManagerV1}/";
            } else {
                parent.layer.msg(data.message);
            }
            if(data.code >= 0){
                parent.findData();
                parent.layer.close(parent.tableIndex);
            }
        }
    });
    
/*    
    $("#saveAndUpdateFrom").attr("action", "${userInfoV1}/baseGame" + action[i]);
    if ($("#isReCommit").val() == 0) {
        $("#isReCommit").val(1);
        //loading层
        var index = layer.load(0, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        //$("#saveAndUpdateFrom").submit();
    } */
}

function checkGameName() {
    //渠道游戏ID
    var gameId = $.trim($("#gameId").val());
    var areaId = $.trim($("#areaId").val());
    var serverId = $.trim($("#serverId").val());
    var currentGoodsId = $("#currentGoodsId").val();
    var targetGoodsId = $("#targetGoodsId").val();

    var flag = $("#flag").val();
    var rowId = $("#rowId").val();;
    var aa = false;
    if (CheckUtil.isNotNull(targetGoodsId)) {
        $.ajax({
            type: "post",
            cache: false,
            url: "${userInfoV1}/gameGoodsSwap/checkTypeIsExist",
            data:{rowId:rowId, gameId:gameId, areaId:areaId, serverId:serverId, currentGoodsId:currentGoodsId, targetGoodsId:targetGoodsId},
            dataType:"json",
            async:false,
            success: function(data) {
                aa = data;
            }
        });
    }
    return aa;  
}
</script>
</html>